<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/16
 * Time: 14:09
 */

namespace App\LogicService;

use App\LogicService\Contracts\UserGroupMemberLogicContract;
use App\Models\UserGroupMember;


class UserGroupMemberLogic extends BaseLogic implements UserGroupMemberLogicContract
{

    public function addUserlist($group_id, $accounts)
    {
        $result = [];
        foreach ($accounts as $value) {
            $save_data = ['group_id' => $group_id, 'user_account' => $value];
            $id = UserGroupMember::create($save_data);
            $result[] = $id;
        }
        return $result;
    }

    public function deleteUserList($group_id)
    {
        $result = UserGroupMember::where('group_id', $group_id)->delete();
        return $result;
    }


    public function getGroupMemberList($group_id)
    {
        $result = UserGroupMember::where('group_id', $group_id)->get();
        return $result;
    }

    public function store($group_id, $user_account)
    {
        //判断用户是否已经在群组
        $groupMember = $this->show($group_id, $user_account);

        if(!$groupMember){
            $save_data = ['group_id' => $group_id, 'user_account' => $user_account];
            $result = UserGroupMember::create($save_data);
        }else{
            $result = false;
        }

        return $result;
    }

    public function show($group_id, $user_account){

        $result = UserGroupMember::where('group_id', $group_id)
            ->where('user_account', $user_account)
            ->first();
        return $result;
    }



    public function delete($group_id, $user_account){

        $result = UserGroupMember::where('group_id', $group_id)
            ->where('user_account', $user_account)
            ->delete();
        return $result;
    }

}